package com.lhf66.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.lhf66.domain.CatItems;
import com.lhf66.domain.Meta;
import com.lhf66.domain.School;
import com.lhf66.domain.SwiperData;
import com.lhf66.service.SchoolService;
import com.lhf66.service.impl.HomeServiceImpl;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

@Controller
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private HomeServiceImpl homeService;

    @Autowired
    private Meta meta;

    @RequestMapping("/catitems")
    @ResponseBody
    public Object catitems() throws IOException {
        List<CatItems> catItemsList = homeService.getcatItemsList();
        System.out.println(catItemsList);


        JSONObject jsonObject = new JSONObject();

        // 讲对象数组转换成json数组
        JSONArray jsonArray = JSONArray.fromObject(catItemsList);  // 获取json数组
        jsonObject.put("message", jsonArray);

        meta.setMsg("获取成功");
        meta.setStatus(200);
        jsonObject.put("meta",meta);

        // System.out.println(jsonObject);

        return jsonObject;
    }

    @RequestMapping("/swiperdata")
    @ResponseBody
    public Object swiperdata() throws IOException {
        List<SwiperData> swiperDataList = homeService.getswiperDataList();
        // System.out.println(swiperDataList);

        JSONObject jsonObject = new JSONObject();

        JSONArray jsonArray = JSONArray.fromObject(swiperDataList);  // 获取json数组
        jsonObject.put("message", jsonArray);

        meta.setMsg("获取成功");
        meta.setStatus(200);
        jsonObject.put("meta",meta);

        // System.out.println(jsonObject);

        return jsonObject;
    }
}
